package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.FixedAssetsDto;
import com.qfedu.hotelssm.entity.Employee;
import com.qfedu.hotelssm.entity.FixedAssets;
import com.qfedu.hotelssm.service.intf.FixedAssetsService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/fixed/")
public class FixedAssetsController {
    @Autowired
    private FixedAssetsService service;

    @PostMapping("save.do")
    public R save(FixedAssets fixedAssets){
        return service.save(fixedAssets);
    }

    @GetMapping("delete.do")
    public R delete(int id){
        return service.delete(id);
    }

    @PostMapping("update.do")
    public R update(FixedAssets fixedAssets){
        return service.update(fixedAssets);
    }

    @GetMapping("page.do")
    public PageBean selectPage(String info, int page, int limit){
        return service.selectPage(info, page, limit);
    }

    @PostMapping("delids.do")
    public R deleteByIds(int[] ids){
        return service.deleteByIds(ids);
    }

    //导出
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        List<FixedAssetsDto> list = service.download();
        response.setHeader("content-disposition", "attachment;filename=fixed"+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(),FixedAssetsDto.class).sheet("财务表").doWrite(list);
    }

    //导入
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
}
